﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;

namespace BLL.Order
{
    public class T_PUB_ORDER
    {
        private readonly DALL.Order.T_PUB_ORDER dal = new DALL.Order.T_PUB_ORDER();

        /// <summary>
        /// 获取订单跟踪提醒
        /// </summary>
        /// <param name="branch_id"></param>
        /// <param name="user_id"></param>
        /// <returns></returns>
        public DataTable GetRemindOrderList(int branch_id, int user_id)
        {
            return dal.GetRemindOrderList(branch_id, user_id);
        }


        public void AddOrder(Model.Order.T_PUB_ORDER model, Model.View.T_PUB_CUSTOMERINFO coustomerModel, Dictionary<string, string> ProjectID_No)
        {
            dal.AddOrder(model, coustomerModel, ProjectID_No);
        }

        public void EditOrder(Model.Order.T_PUB_ORDER model, Model.View.T_PUB_CUSTOMERINFO coustomerModel, Dictionary<string, string> ProjectID_No, decimal flOldMoney, string strUserId)
        {
            string strProject = model.ORDER_ID;
            List<Model.Order.OrderProjectChange> changeModelList = new List<Model.Order.OrderProjectChange>();
            BLL.Order.T_PUB_Order_Project oldDataBll = new T_PUB_Order_Project();
            List<Model.Order.T_PUB_Order_Projectcs> oldData = oldDataBll.GetOrderProject(strProject);
            string strProjectID;
            Model.Order.T_PUB_Order_Projectcs oldProjectModel;
            //遍历新保存数据
            foreach (KeyValuePair<string, string> item in ProjectID_No)
            {
                Model.Order.OrderProjectChange changeModel = new Model.Order.OrderProjectChange();
                strProjectID = item.Key;
                oldProjectModel = oldData.Find(p => p.ProjectID == strProjectID);
                if (oldProjectModel == null)  //新添加的在原始数据中无法找到说明是新添加的
                {
                    changeModel.strFlag = "1";
                    changeModel.ProjectID = strProjectID;
                    changeModel.ProjectNum = item.Value;
                    changeModelList.Add(changeModel);
                }
                else
                {
                    if (oldProjectModel.Number.ToString() != item.Value)  //数量不同有编辑
                    {
                        changeModel.strFlag = "2";
                        changeModel.ProjectID = strProjectID;
                        changeModel.ProjectNum = item.Value;
                        changeModel.ID = oldProjectModel.id.ToString();
                        changeModelList.Add(changeModel);
                    }
                }

            }
            //遍历原始数据查找删除的记录
            foreach (Model.Order.T_PUB_Order_Projectcs item in oldData)
            {
                strProjectID = item.ProjectID;
                if (!ProjectID_No.ContainsKey(strProjectID))
                {
                    Model.Order.OrderProjectChange changeModel = new Model.Order.OrderProjectChange();
                    changeModel.strFlag = "0";
                    changeModel.ID = item.id.ToString();
                    changeModel.ProjectID = item.ProjectID;
                    changeModel.ProjectNum = item.Number.ToString();
                    changeModelList.Add(changeModel);
                }
            }
            //是否修改定金
            if (Convert.ToDecimal(model.DingJin) == flOldMoney)
            {
                flOldMoney = 0;
            }
            dal.EditOrder(model, coustomerModel, changeModelList, strUserId, flOldMoney);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="strBranchID"></param>
        /// <param name="strReception"></param>
        /// <param name="strBegionDate"></param>
        /// <param name="strEndDate"></param>
        /// <param name="strType"></param>
        /// <param name="sdtrGROOM_NAME"></param>
        /// <param name="strBRIDE_NAME"></param>
        /// <returns></returns>
        public DataTable GetOrderList(string strBranchID, string strReception, string strBegionDate, string strEndDate, string strType, string sdtrGROOM_NAME, string strBRIDE_NAME)
        {
            StringBuilder strWhere = new StringBuilder();
            if (strBranchID != "")
            {
                strWhere.Append(string.Format(" and o.BranchID='{0}'", strBranchID));
            }
            if (strReception != "")
            {
                strWhere.Append(string.Format(" and o.Reception='{0}'", strReception));
            }
            if (strBegionDate != "")
            {
                strWhere.Append(string.Format(" and o.OrderDate>='{0}'", strBegionDate));
            }
            if (strEndDate != "")
            {
                strWhere.Append(string.Format(" and o.OrderDate<='{0}'", strEndDate));
            }
            if (strType != "")
            {
                strWhere.Append(string.Format(" and d.ID='{0}'", strType));
            }
            if (sdtrGROOM_NAME != "")
            {
                strWhere.Append(string.Format(" and c.GROOM_NAME like'%{0}%'", sdtrGROOM_NAME));
            }
            if (strBRIDE_NAME != "")
            {
                strWhere.Append(string.Format(" and c.BRIDE_NAME like'%{0}%'", strBRIDE_NAME));
            }
            return dal.GetOrderList(strWhere.ToString());
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.Order.T_PUB_ORDER GetModelByID(string ORDER_ID)
        {
            return dal.GetModelByID(ORDER_ID);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.Order.T_PUB_ORDER model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 交款添加记录
        /// </summary>
        /// <param name="money"></param>
        /// <param name="strOrderID"></param>
        /// <param name="strUserID"></param>
        /// <param name="strOther"></param>
        public void UpdateMoney(decimal money, string strOrderID, string strUserID, string strOther)
        {
            dal.UpdateMoney(money, strOrderID, strUserID, strOther);
        }

        //获得交款日志
        public DataTable GetAmountLog(string strOrderID)
        {
            return dal.GetAmountLog(strOrderID);
        }
        //项目日志

        public DataTable GetProjectLog(string strOrderID)
        {
            return dal.GetProjectLog(strOrderID);
        }
        public bool UpataeCount(string strid)
        {
            return dal.UpataeCount(strid);
        }
    }
}
